package com.lianzai.ticket.service.impl;

import com.lianzai.ticket.dao.GiftDao;
import com.lianzai.ticket.dao.TicketDao;
import com.lianzai.ticket.model.*;
import com.lianzai.ticket.service.TicketService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class TicketServiceImpl implements TicketService {
	@Autowired
	private TicketDao ticketDao;

	@Autowired
	private GiftDao giftDao;

	@Override
	public List<Ticket> list(Map<String, Object> map){
		List<Ticket> reslutList = ticketDao.list(map);
		for(int i=0;i<reslutList.size();i++) {
			Ticket ticket = reslutList.get(i);
			Integer ticketId = ticket.getId();
			List<Gift> giftList = giftDao.queryGiftList(ticketId);
			String segMsg = "";
			for(int j=0;j<giftList.size();j++){
				Long ticketBuyNum = giftList.get(j).getTicketBuyNum();
				Long ticketGiftNum = giftList.get(j).getTicketGiftNum();
				if("".equals(segMsg)){
					segMsg = "<span>满：</span><span style='color:Red;font-size:16px;font-weight:bold'>"+ticketBuyNum+"</span><span>&nbsp;张，送：</span><span style='color:Red;font-size:16px;font-weight:bold'>"+ticketGiftNum+"</span><span>&nbsp;张</span>";
				}else{
					segMsg = segMsg + "<br><span>满：</span><span style='color:Red;font-size:16px;font-weight:bold'>"+ticketBuyNum+"</span><span>&nbsp;张，送：</span><span style='color:Red;font-size:16px;font-weight:bold'>"+ticketGiftNum+"</span><span>&nbsp;张</span>";
				}
			}
			if(!"".equals(segMsg)){
				ticket.setGift(segMsg);
			}
		}
		return reslutList;
	}

	@Override
	public int count(Map<String, Object> map){
		return ticketDao.count(map);
	}

	@Override
	public int remove(Long id){
		giftDao.remove(Integer.parseInt(id+""));
		return ticketDao.remove(id);
	}

	@Override
	public List<Map<String, Object>> queryTicektShopUsedList(Map<String, Object> map){
		return ticketDao.queryTicektShopUsedList(map);
	}

	@Override
	public List<Map<String, Object>> listRel(Map<String, Object> map){
		return ticketDao.listRel(map);
	}



	@Override
	public int save(Ticket ticket){
		//新增停车券
		int key = ticketDao.save(ticket);
		//新增停车券规则
		String buyNums = ticket.getBuyNums();
		if(buyNums != null && !"".equals(buyNums)){
			String giftNums = ticket.getGiftNums();
			String[] buyNumsArray = buyNums.split(",");
			String[] giftNumsArray = giftNums.split(",");
			for(int i=0;i<buyNumsArray.length;i++){
				Gift gift = new Gift();
				gift.setTicketId(Long.parseLong(ticket.getId()+""));
				gift.setTicketBuyNum(Long.parseLong(buyNumsArray[i]));
				gift.setTicketGiftNum(Long.parseLong(giftNumsArray[i]));
				giftDao.save(gift);
			}
		}
        return key;
	}

	@Override
	public int update(Ticket ticket){
		//删除挂念规则
		giftDao.remove(ticket.getId());
        //新增停车券规则
		String buyNums = ticket.getBuyNums();
		if(buyNums != null && !"".equals(buyNums)){
			String giftNums = ticket.getGiftNums();
			String[] buyNumsArray = buyNums.split(",");
			String[] giftNumsArray = giftNums.split(",");
			for(int i=0;i<buyNumsArray.length;i++){
				Gift gift = new Gift();
				gift.setTicketId(Long.parseLong(ticket.getId()+""));
				gift.setTicketBuyNum(Long.parseLong(buyNumsArray[i]));
				gift.setTicketGiftNum(Long.parseLong(giftNumsArray[i]));
				giftDao.save(gift);
			}
		}
		return ticketDao.update(ticket);
	}

	@Override
	public Ticket get(Long id){
		return ticketDao.get(id);
	}

	@Override
	public int bind(Long communityId,Long ticketId,int type){
		Map map = new HashedMap();
		map.put("communityId",communityId);
		map.put("ticketId",ticketId);
		int ret = 0;
		if(type == 1){//如果是绑定
			//ticketDao.delBind(map);
			//先解绑小区关联的所有停车券
			ticketDao.delAllBind(map);
			ret = ticketDao.bind(map);
		}else{
			ret =ticketDao.delBind(map);//解绑
		}
		return ret;
	}
	@Override
	public List<Ticket> check(String name) {
		return ticketDao.check(name);
	}
	@Override
	@Transactional
	public Ticket getTicketByWXId(String openId)
	{

		//查询停车券基本信息
		Ticket ticket = ticketDao.getTicketSpecByWXId(openId);
		if (ticket == null)
		{
			return null;
		}

		//查询停车券优惠规则
		List<TicketDiscount> discounts = ticketDao.getTicketDiscountByWXId(openId);

		//将优惠规则与停车券绑定
		ticket.setDiscountRules(discounts);

		return ticket;
	}

	public MerchantTicketDO getTicketOverview(Integer userId)
	{
		return ticketDao.getTicketOverview(userId);
	}

	public List<Map<String, Object>> getTicketOrderList(Map<String, Object> map)
	{
		return ticketDao.getTicketOrderList(map);
	}

	public int countTicketOrder(Integer userId)
	{
		return ticketDao.countTicketOrder(userId);
	}

	public List<TicketUseInfoDO> getTicketUseList(Map<String, Object> map)
	{
		return ticketDao.getTicketUseList(map);
	}
	public int countTicketUse(Map<String, Object> map)
	{

		return ticketDao.countTicketUse(map);
	}

	public Ticket getTicketSpecByUserAndCommunity(Map<String, Object> map)
	{
		return ticketDao.getTicketSpecByUserAndCommunity(map);
	}
}
